package com.baohua.controller;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baohua.exception.BaohuaSystemException;
import com.baohua.model.MaterielInformation;
import com.baohua.service.MaterielInformationService;

@Controller
@RequestMapping("/materiel")
public class MaterielInfoController {
 
	/**
	 * 跳转到供应商主页面
	 * @return
	 */
	@RequestMapping("/index")
	public String index() {
		return "materielInfo";
	}
	
	/**
	 * 根据条件查询供应商列表
	 * @param request
	 * @return
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@RequestMapping("/list")
	public @ResponseBody Map queryList(MaterielInformation m, HttpServletRequest request){
		int page = Integer.parseInt(request.getParameter("page"));
		int pageSize = Integer.parseInt(request.getParameter("pagesize"));
		
		Map map = materielInformationService.getMaterielInfomation(m, page, pageSize);
		return map;
	}
	
	@RequestMapping(value="loadDirctionary/{id}", method =RequestMethod.GET)
	public @ResponseBody List<String> loadDirctionary(@PathVariable long id) {
		List<String> list = materielInformationService.queryDictionatyByPid(id);
		return list;
	}
	
	@RequestMapping("/add")
	public String addMaterielInformation(MaterielInformation m, Model model) {
     try{
    	 materielInformationService.addMaterielInfomation(m);
	  } catch (BaohuaSystemException e) {
	 		logger.info(e.getMessage());
	 		model.addAttribute("errorMessage", e.getMessage());
	 		return "forward:/system/error";
	  } 
	return "redirect:/materiel/index";
	}
	
	@RequestMapping("/update")
	public String updateMaterielInformation(MaterielInformation m, Model model) {
     try{
    	 materielInformationService.updateMaterielInfomation(m);
	  } catch (BaohuaSystemException e) {
	 		logger.info(e.getMessage());
	 		model.addAttribute("errorMessage", e.getMessage());
	 		return "forward:/system/error";
	  } 
	return "redirect:/materiel/index";
	}
	
	@RequestMapping(value="/del/{rowsNum}", method=RequestMethod.GET)
	public @ResponseBody String delMaterielInformation(@PathVariable String rowsNum) {
		ObjectMapper objectMapper = new ObjectMapper();
		try {
			String[] mIds = objectMapper.readValue(rowsNum, String[].class);
			for(String mId : mIds) {
				materielInformationService.deleteMaterielInfomation(Integer.parseInt(mId));
			}
		} catch (JsonParseException e) {
			logger.info(e.getMessage());
			return "{message:systemerror}";
		} catch (JsonMappingException e) {
			logger.info(e.getMessage());
			return "{message:'systemerror'}";
		}  catch (BaohuaSystemException e) {
			logger.info(e.getMessage());
			return "{message:'error'}";
		} catch (IOException e) {
			logger.info(e.getMessage());
			return "{message:'systemerror'}";
		}
		return "{message : 'success'}";
	}
	
	private static final Logger logger = Logger.getLogger(MaterielInfoController.class.getName());
	private MaterielInformationService materielInformationService;
	
	public MaterielInformationService getMaterielInformationService() {
		return materielInformationService;
	}

	@Resource(name="materielInfomationService")
	public void setMaterielInformationService(
			MaterielInformationService materielInformationService) {
		this.materielInformationService = materielInformationService;
	}
	
	
}
